<?php
/**
 * -----------------------------------------------------------------------------
 *
 * SyL - PHP Application Library
 *
 * PHP version 5 (>= 5.2.10)
 *
 * Copyright (C) 2006-2011 k.watanabe
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * -----------------------------------------------------------------------------
 * @package    SyL.Lib
 * @subpackage SyL.Lib.Xml
 * @author     Koki Watanabe <k.watanabe@syl.jp>
 * @copyright 2006-2012 k.watanabe
 * @license    http://www.opensource.org/licenses/lgpl-license.php
 * @version    CVS: $Id:$
 * @link       http://syl.jp/
 * -----------------------------------------------------------------------------
 */

/** 
 * XSLT 適用クラス
 *
 * @package    SyL.Lib
 * @subpackage SyL.Lib.Xml
 * @author     Koki Watanabe <k.watanabe@syl.jp>
 * @copyright 2006-2012 k.watanabe
 * @license    http://www.opensource.org/licenses/lgpl-license.php
 * @version    CVS: $Id:$
 * @link       http://syl.jp/
 */
class SyL_XmlXslt
{
    /**
     * データに対して XSLT を適用する
     *
     * @param string XMLデータ
     * @param string XSLデータ
     * @param array パラメータ
     * @return string XSLT適用後データ
     */
    public static function apply($xml_data, $xsl_data, array $parameters=array())
    {
        $xml = new DOMDocument();
        $xml->loadXML($xml_data);
        $xsl = new DOMDocument();
        $xsl->loadXML($xsl_data);

        return self::transform($xml, $xsl, $parameters);
    }

    /**
     * XSLT を適用する
     *
     * @param DOMDocument XMLデータ
     * @param DOMDocument XSLデータ
     * @param array パラメータ
     * @return string XSLT適用後データ
     */
    public static function transform(DOMDocument $xml, DOMDocument $xsl, array $parameters=array())
    {
        $xslt = new XSLTProcessor();
        $xslt->importStyleSheet($xsl);

        foreach ($parameters as $name => $value) {
            $xslt->setParameter('', $name, $value);
        }

        return $xslt->transformToXML($xml);
    }

}
